package com.zhxin.nettylab.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

/**
 * @ClassName HttpServerHandler
 * @Description //HttpServerHandler
 * @Author singleZhang
 * @Email 405780096@qq.com
 * @Date 2021/2/25 0025 上午 11:49
 **/
public class HttpServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest  msg) throws Exception {
        //if(msg instanceof HttpRequest){
            FullHttpRequest  request = msg;

            System.out.println(request.method().name());

            System.out.println(request.uri());

            System.out.println(request.content().toString(CharsetUtil.UTF_8));
       // }

       // if(msg instanceof HttpContent){
            //HttpContent content = (HttpContent) msg;

            //ByteBuf buf = content.content();
            //System.out.println(buf.toString(CharsetUtil.UTF_8));

            ByteBuf resBuf = Unpooled.copiedBuffer("hello,server test!",CharsetUtil.UTF_8);
            FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK,resBuf);
            response.headers().add(HttpHeaderNames.CONTENT_TYPE,"text/plain");
            response.headers().add(HttpHeaderNames.CONTENT_LENGTH,resBuf.readableBytes());

            ctx.writeAndFlush(response);
       // }
    }
}
